package com.poisonh.unifiedauth.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.poisonh.unifiedauth.dao.UserinfoDaoImpl;
import com.poisonh.unifiedauth.model.UserInfoModel;
import com.poisonh.unifiedauth.utils.ActivityStack;
import com.poisonh.unifiedauth.utils.ConstantUitls;
import com.poisonh.unifiedauth.utils.PreferencesUtils;
import com.poisonh.unifiedauth.utils.TimeUtils;

import java.util.List;

/**
 * 引导页
 * Created by PoisonH on 2016/7/26.
 */
public class SplashActivity extends Activity
{
    private String mStrId;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
      //  setContentView(R.layout.activity_splash);
//        new Handler().postDelayed(new Runnable()
//        {
//            @Override
//            public void run()
//            {
//                execute();
//            }
//        }, 500);
        execute();

    }

    private void execute()
    {
        ActivityStack.getAppManager().addActivity(this);

        /**
         * 三方SDK调用年轮认证APP所需要传递的参数
         */
        ConstantUitls.SDK_APPID = this.getIntent().getStringExtra("appid");
        ConstantUitls.SDK_APPNAME = this.getIntent().getStringExtra("appname");
        ConstantUitls.SDK_APPIMG = this.getIntent().getStringExtra("appimg");

        if (!TextUtils.isEmpty(ConstantUitls.SDK_APPID) && !TextUtils.isEmpty(ConstantUitls.SDK_APPNAME) && !TextUtils.isEmpty(ConstantUitls.SDK_APPIMG))
        {
            checkUserState();
        } else
        {
            checkUserState();
        }
    }

    private void checkUserState()
    {
        //获取用户id
        mStrId = PreferencesUtils.getString(this, "ID");
        if (mStrId == null)
        {
            startActivity(new Intent(SplashActivity.this, LoginActivity.class));
            finish();
        } else if (mStrId != null)
        {
         //   ConstantUitls.mStrId = mStrId;

            if (checkToken(mStrId))
            {
                startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                finish();
            } else
            {
                ConstantUitls.TYPE = 4;
                startActivity(new Intent(SplashActivity.this, SettingPinCodeActivity.class));
                finish();
            }
        }
    }

    /**
     * 根据di判断当前用户token是否存在并且是否过期
     *
     * @param id
     * @return
     */
    private boolean checkToken(String id)
    {
        try
        {
            UserinfoDaoImpl mUserinfoDaoImpl = new UserinfoDaoImpl(SplashActivity.this);
            List<UserInfoModel> list = mUserinfoDaoImpl.queryReturnList(id);
            String token = list.get(0).getmStrToken();
            if (TextUtils.isEmpty(token))
            {
                return true;
            } else
            {
                String mStrOldDate = list.get(0).getmDeadDate();
                //                    ConstantUitls.mStrToken = token;
//                    ConstantUitls.mStrPhone = list.get(0).getmStrPhone();
//                    ConstantUitls.mStrName = list.get(0).getmStrUserName();
//                    ConstantUitls.mStrHeadUrl = list.get(0).getmStrHeadUrl();
//                    if (list.get(0).getmStrReallyName() != null && list.get(0).getmStrCard() != null)
//                    {
//                        ConstantUitls.mStrReallyName = list.get(0).getmStrReallyName();
//                        ConstantUitls.mStrCard = list.get(0).getmStrCard();
//                    }
                return TimeUtils.checkDate(mStrOldDate);
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        ActivityStack.getAppManager().finishActivity(this);
    }

}
